package com.ruoyi.cms.controller;

import com.alibaba.fastjson2.JSON;
import com.github.pagehelper.page.PageMethod;
import com.ruoyi.cms.domain.*;
//import com.ruoyi.cms.domain.vo.CmsWzkfModuleVo;
import com.ruoyi.cms.service.*;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.utils.PageUtils.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

import static com.ruoyi.common.utils.PageUtils.clearPage;
import static com.ruoyi.common.utils.PageUtils.startPage;

@Controller
public class CmsWebController {
    @Autowired
    private ICmsSiteInfoService cmsSiteInfoService;
    @Autowired
    private ICmsSiteModuleService cmsSiteModuleService;
    @Autowired
    private ICmsSiteBannerService cmsSiteBannerService;

//    @Autowired
//    private ICmsWzkfModuleService cmsWzkfModuleService;
//
//    @Autowired
//    private ICmsWzkfBannerService cmsWzkfBannerService;
//
//
//    @Autowired
//   private ICmsAppModuleService cmsAppModuleService;
//
//
//   @Autowired
//    private ICmsAppBannerService cmsAppBannerService;
//
//
//    @Autowired
//    private ICmsXcxkfModuleService cmsXcxkfModuleService;
//
//
//    @Autowired
//    private ICmsXcxkfBannerService cmsXcxkfBannerService;
//
//
//    @Autowired
//    private ICmsAlModuleService cmsAlModuleService;
//
//
//    @Autowired
//    private ICmsAlBannerService cmsAlBannerService;
//
//
//
//    @Autowired
//    private ICmsGywmModuleService cmsGywmModuleService;
//
//
//    @Autowired
//    private ICmsGywmBannerService cmsGywmBannerService;

    private CmsSiteInfo handPubData(Model model) {
        CmsSiteInfo info = cmsSiteInfoService.selectUseCmsSiteInfo();
        CmsSiteModule head = cmsSiteModuleService.selectCmsSiteModuleTypeValue(info.getModuleType(), "m_head");
        CmsSiteModule footer = cmsSiteModuleService.selectCmsSiteModuleTypeValue(info.getModuleType(), "m_footer");
        model.addAttribute("info", info);
        model.addAttribute("head", head);
        model.addAttribute("footer", footer);
        return info;
    }



    @GetMapping("/pub_head")
    public String pubHead(Model model,@RequestParam(value = "searchKey",required = false) String searchKey) {


        CmsSiteInfo info = handPubData(model);
        String moduleType = info.getModuleType();
        CmsSiteModule paramsModule = new CmsSiteModule();
        paramsModule.setType(moduleType);
        paramsModule.setStatus(Constants.SUCCESS);



        List<CmsSiteModule> modules = cmsSiteModuleService.selectCmsSiteModuleList(paramsModule);
        modules.sort(Comparator.comparing(CmsSiteModule::getSort));
        //搜索
        List<Map<String, Object>> moduleInfos = new ArrayList<>();
        for (CmsSiteModule m : modules) {
            CmsSiteBanner paramBanner = new CmsSiteBanner();
            if(searchKey!=null){
                paramBanner.setTitle(searchKey);
            }

            List<CmsSiteBanner> banners = cmsSiteBannerService.selectCmsSiteBannerList(paramBanner);
            banners.sort(Comparator.comparing(CmsSiteBanner::getSort));
            Map<String, Object> r = new HashMap<>(3);
            r.put("type", m.getValue());
            r.put("module", m);
            r.put("banners", banners);
            moduleInfos.add(r);
        }
        model.addAttribute("modules", moduleInfos);
//        System.out.println(JSON.toJSON(moduleInfos));
        return "search";
    }





    @GetMapping("/pub_footer")
    public String pubFooter(Model model,@RequestParam(value = "searchKey",required = false) String searchKey) {
        CmsSiteInfo info = handPubData(model);
        String moduleType = info.getModuleType();
        CmsSiteModule paramsModule = new CmsSiteModule();
        paramsModule.setType(moduleType);
        paramsModule.setStatus(Constants.SUCCESS);
        List<CmsSiteModule> modules = cmsSiteModuleService.selectCmsSiteModuleList(paramsModule);
        modules.sort(Comparator.comparing(CmsSiteModule::getSort));
        //搜索
        List<Map<String, Object>> moduleInfos = new ArrayList<>();
        for (CmsSiteModule m : modules) {
            CmsSiteBanner paramBanner = new CmsSiteBanner();
            if(searchKey!=null){
                paramBanner.setTitle(searchKey);
            }

            List<CmsSiteBanner> banners = cmsSiteBannerService.selectCmsSiteBannerList(paramBanner);
            banners.sort(Comparator.comparing(CmsSiteBanner::getSort));
            Map<String, Object> r = new HashMap<>(3);
            r.put("type", m.getValue());
            r.put("module", m);
            r.put("banners", banners);
            moduleInfos.add(r);
        }
        model.addAttribute("modules", moduleInfos);
        System.out.println(JSON.toJSON(moduleInfos));
        return "search";
    }




    @GetMapping("/index")
    public String index(Model model) {
        //获取站点基础信息
        CmsSiteInfo info = handPubData(model);
        //封装查询条件
        String moduleType = info.getModuleType();
        CmsSiteModule paramsModule = new CmsSiteModule();
        paramsModule.setType(moduleType);
        paramsModule.setStatus(Constants.SUCCESS);
        //开始查询
        List<CmsSiteModule> modules = cmsSiteModuleService.selectCmsSiteModuleList(paramsModule);
        modules.sort(Comparator.comparing(CmsSiteModule::getSort)); //排序

        //存放最终所有数据
        List<Map<String, Object>> moduleInfos = new ArrayList<>();
        //遍历大表
        for (CmsSiteModule m : modules) {
            //封装条件
            CmsSiteBanner paramBanner = new CmsSiteBanner();
            paramBanner.setModuleId(m.getId()); //大表小表对应关系
            paramBanner.setStatus(Constants.SUCCESS);
            //开始查询大表中某条数据对应的小表数据（多个）
            List<CmsSiteBanner> banners = cmsSiteBannerService.selectCmsSiteBannerList(paramBanner);
            banners.sort(Comparator.comparing(CmsSiteBanner::getSort)); //给获取的数据排序
            //开始封装数据
            Map<String, Object> r = new HashMap<>(3);
            r.put("type", m.getValue());
            r.put("module", m);
            r.put("banners", banners);
            //将封装好的某个大表数据放进去
            moduleInfos.add(r);
        }
        //将获取到的所有数据返回到前端
        model.addAttribute("modules", moduleInfos);
        System.out.println(JSON.toJSON(moduleInfos));
        return "index";
    }

    @GetMapping("/blog")
    public String blog(Model model) {
        //获取站点基础信息
        CmsSiteInfo info = handPubData(model);
        //封装查询条件
        String moduleType = info.getModuleType();
        CmsSiteModule paramsModule = new CmsSiteModule();
        paramsModule.setType(moduleType);
        paramsModule.setStatus(Constants.SUCCESS);
        //开始查询
        List<CmsSiteModule> modules = cmsSiteModuleService.selectCmsSiteModuleList(paramsModule);
        modules.sort(Comparator.comparing(CmsSiteModule::getSort)); //排序

        //存放最终所有数据
        List<Map<String, Object>> moduleInfos = new ArrayList<>();
        //遍历大表
        for (CmsSiteModule m : modules) {
            //封装条件
            CmsSiteBanner paramBanner = new CmsSiteBanner();
            paramBanner.setModuleId(m.getId()); //大表小表对应关系
            paramBanner.setStatus(Constants.SUCCESS);
            //开始查询大表中某条数据对应的小表数据（多个）
            List<CmsSiteBanner> banners = cmsSiteBannerService.selectCmsSiteBannerList(paramBanner);
            banners.sort(Comparator.comparing(CmsSiteBanner::getSort)); //给获取的数据排序
            //开始封装数据
            Map<String, Object> r = new HashMap<>(3);
            r.put("type", m.getValue());
            r.put("module", m);
            r.put("banners", banners);
            //将封装好的某个大表数据放进去
            moduleInfos.add(r);
        }
        //将获取到的所有数据返回到前端
        model.addAttribute("modules", moduleInfos);
        System.out.println(JSON.toJSON(moduleInfos));
        return "blog";
    }


    @GetMapping("/about")
    public String about(Model model) {
        //获取站点基础信息
        CmsSiteInfo info = handPubData(model);
        //封装查询条件
        String moduleType = info.getModuleType();
        CmsSiteModule paramsModule = new CmsSiteModule();
        paramsModule.setType(moduleType);
        paramsModule.setStatus(Constants.SUCCESS);
        //开始查询
        List<CmsSiteModule> modules = cmsSiteModuleService.selectCmsSiteModuleList(paramsModule);
        modules.sort(Comparator.comparing(CmsSiteModule::getSort)); //排序

        //存放最终所有数据
        List<Map<String, Object>> moduleInfos = new ArrayList<>();
        //遍历大表
        for (CmsSiteModule m : modules) {
            //封装条件
            CmsSiteBanner paramBanner = new CmsSiteBanner();
            paramBanner.setModuleId(m.getId()); //大表小表对应关系
            paramBanner.setStatus(Constants.SUCCESS);
            //开始查询大表中某条数据对应的小表数据（多个）
            List<CmsSiteBanner> banners = cmsSiteBannerService.selectCmsSiteBannerList(paramBanner);
            banners.sort(Comparator.comparing(CmsSiteBanner::getSort)); //给获取的数据排序
            //开始封装数据
            Map<String, Object> r = new HashMap<>(3);
            r.put("type", m.getValue());
            r.put("module", m);
            r.put("banners", banners);
            //将封装好的某个大表数据放进去
            moduleInfos.add(r);
        }
        //将获取到的所有数据返回到前端
        model.addAttribute("modules", moduleInfos);
        System.out.println(JSON.toJSON(moduleInfos));
        return "about";
    }




    @GetMapping("/contact")
    public String contact(Model model) {
        //获取站点基础信息
        CmsSiteInfo info = handPubData(model);
        //封装查询条件
        String moduleType = info.getModuleType();
        CmsSiteModule paramsModule = new CmsSiteModule();
        paramsModule.setType(moduleType);
        paramsModule.setStatus(Constants.SUCCESS);
        //开始查询
        List<CmsSiteModule> modules = cmsSiteModuleService.selectCmsSiteModuleList(paramsModule);
        modules.sort(Comparator.comparing(CmsSiteModule::getSort)); //排序

        //存放最终所有数据
        List<Map<String, Object>> moduleInfos = new ArrayList<>();
        //遍历大表
        for (CmsSiteModule m : modules) {
            //封装条件
            CmsSiteBanner paramBanner = new CmsSiteBanner();
            paramBanner.setModuleId(m.getId()); //大表小表对应关系
            paramBanner.setStatus(Constants.SUCCESS);
            //开始查询大表中某条数据对应的小表数据（多个）
            List<CmsSiteBanner> banners = cmsSiteBannerService.selectCmsSiteBannerList(paramBanner);
            banners.sort(Comparator.comparing(CmsSiteBanner::getSort)); //给获取的数据排序
            //开始封装数据
            Map<String, Object> r = new HashMap<>(3);
            r.put("type", m.getValue());
            r.put("module", m);
            r.put("banners", banners);
            //将封装好的某个大表数据放进去
            moduleInfos.add(r);
        }
        //将获取到的所有数据返回到前端
        model.addAttribute("modules", moduleInfos);
        System.out.println(JSON.toJSON(moduleInfos));
        return "contact";
    }


/*
    @GetMapping("/search")
    public String search(Model model) {
        //获取站点基础信息
        CmsSiteInfo info = handPubData(model);
        //封装查询条件
        String moduleType = info.getModuleType();
        CmsSiteModule paramsModule = new CmsSiteModule();
        paramsModule.setType(moduleType);
        paramsModule.setStatus(Constants.SUCCESS);
        //开始查询
        List<CmsSiteModule> modules = cmsSiteModuleService.selectCmsSiteModuleList(paramsModule);
        modules.sort(Comparator.comparing(CmsSiteModule::getSort)); //排序

        //存放最终所有数据
        List<Map<String, Object>> moduleInfos = new ArrayList<>();
        //遍历大表
        for (CmsSiteModule m : modules) {
            //封装条件
            CmsSiteBanner paramBanner = new CmsSiteBanner();
            paramBanner.setModuleId(m.getId()); //大表小表对应关系
            paramBanner.setStatus(Constants.SUCCESS);
            //开始查询大表中某条数据对应的小表数据（多个）
            List<CmsSiteBanner> banners = cmsSiteBannerService.selectCmsSiteBannerList(paramBanner);
            banners.sort(Comparator.comparing(CmsSiteBanner::getSort)); //给获取的数据排序
            //开始封装数据
            Map<String, Object> r = new HashMap<>(3);
            r.put("type", m.getValue());
            r.put("module", m);
            r.put("banners", banners);
            //将封装好的某个大表数据放进去
            moduleInfos.add(r);
        }
        //将获取到的所有数据返回到前端
        model.addAttribute("modules", moduleInfos);
        System.out.println(JSON.toJSON(moduleInfos));
        return "search";
    }*/



    @GetMapping("/cms_casedetail")
    public String casedetail(Model model) {
        CmsSiteInfo info = handPubData(model);
        return "cms_casedetail";
    }

    @GetMapping("/details")
    public String details(Model model,@RequestParam(value="id") long id){
        CmsSiteBanner cmsSiteBanner = cmsSiteBannerService.selectCmsSiteBannerById(id);
        model.addAttribute("data",cmsSiteBanner);
        System.out.println(JSON.toJSON(cmsSiteBanner));
        return "details";
    }



}
